<?php
class Admin_UserController extends Zend_Controller_Action {
	
	public function init() {
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()) {
			$this->view->identity = $auth->getIdentity();
		}
	}
	
	public function createAction() {
		$userForm = new Admin_Form_User();
		if ($this->_request->isPost()) {
			if ($userForm->isValid($_POST)) {
				$userModel = new Model_User();
				$userModel->createUser(
					$userForm->getValue('username'),
					$userForm->getValue('password'),
					$userForm->getValue('first_name'),
					$userForm->getValue('last_name'),
					$userForm->getValue('role')
				);
			return $this->_forward('list');
			}
		}
		$userForm->setAction('/admin/user/create');
		$this->view->form = $userForm;
	}
	
	public function listAction () {
		$currentUsers = Model_User::getUsers();
		if ($currentUsers->count() > 0) {
			$this->view->users = $currentUsers;
		} else {
			$this->view->users = null;
		}
	}
	
	public function updateAction () {
		$userForm = new Admin_Form_User();
		$userForm->setAction('/admin/user/update');
		$userForm->removeElement('password');
		$userModel = new Model_User();
		if ($this->_request->isPost()) {
			if ($userForm->isValid($_POST)) {
				$userModel->updateUser(
					$userForm->getValue('id'),
					$userForm->getValue('username'),
					$userForm->getValue('first_name'),
					$userForm->getValue('last_name'),
					$userForm->getValue('role')
				);
				return $this->_forward('list');
			}
		} else {
			$id = $this->_request->getParam('id');
			$currentUser = $userModel->find($id)->current();
			$userForm->populate($currentUser->toArray());
		}
		$this->view->form = $userForm;
	}
	
	public function passwordAction () {
		$passwordForm = new Admin_Form_User();
		$passwordForm->setAction('/admin/user/password');
		$passwordForm->removeElement('first_name');
		$passwordForm->removeElement('last_name');
		$passwordForm->removeElement('username');
		$passwordForm->removeElement('role');
		$userModel = new Model_User();
		if ($this->_request->isPost()) {
			if ($passwordForm->isValid($_POST)) {
				$userModel->updatePassword(
					$passwordForm->getValue('id'),
					$passwordForm->getValue('password')
				);
				return $this->_forward('list');
			}
		} else {
			$id = $this->_request->getParam('id');
			$currentUser = $userModel->find($id)->current();
			$passwordForm->populate($currentUser->toArray());
		}
		$this->view->form = $passwordForm;
	}
	
	public function deleteAction() {
		$id = $this->_request->getParam('id');
		$userModel = new Model_User();
		$userModel->deleteUser($id);
		return $this->_forward('list');
	}
	
	public function loginAction () {
		$userForm = new Admin_Form_User();
		$userForm->setAction('/admin/user/login');
		$userForm->removeElement('first_name');
		$userForm->removeElement('last_name');
		$userForm->removeElement('role');
		if ($this->_request->isPost() && $userForm->isValid($_POST)) {
			$data = $userForm->getValues();
			//set up the auth adapter
			// get the default db adapter
			$db = Zend_Db_Table::getDefaultAdapter();
			//create the auth adapter
			$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password');
			//set the username and password
			$authAdapter->setIdentity($data['username']);
			$authAdapter->setCredential(md5($data['password']));
			//authenticate
			$result = $authAdapter->authenticate();
			if ($result->isValid()) {
				// store the username, first and last names of the user
				$auth = Zend_Auth::getInstance();
				$storage = $auth->getStorage();
				$storage->write($authAdapter->getResultRowObject(array('username' , 'first_name' , 'last_name', 'role')));
				return $this->_redirect('/admin');
			} else {
				$this->view->loginMessage = "Sorry, your username or password was incorrect";
			}
		}
		$this->view->form = $userForm;
	}
	
	public function logoutAction () {
		$authAdapter = Zend_Auth::getInstance();
		$authAdapter->clearIdentity();
	}
}